package com.kiln.juxingqs.ui

import android.content.Intent
import androidx.lifecycle.Observer
import client.global.commlib.toast.toast
import com.kiln.juxingqs.R
import com.kiln.juxingqs.dialog.JyInputNumDialog
import com.kiln.juxingqs.dialog.PagingFixedBathDialog
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.databinding.ActivityZrdjBinding
import com.kiln.juxingqs.ui.vm.ZrdjViewModel

class ZrDjActivity : BaseMiddleActivity<ZrdjViewModel, ActivityZrdjBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_zrdj
    }

    override fun initView() {
        binding?.apply {
            event = this@ZrDjActivity
            vm = viewModel
        }
    }

    override fun observerUI() {
        viewModel.call.observe(this, Observer {
            it?.apply {
                if (isSuccess) {
                    toast("操作成功")
                    startActivity(Intent(this@ZrDjActivity, DjzrActivity::class.java).apply {
                        putExtra("index", 2)
                    })
                    finish()
                } else {
                    toast(it.message, it.code)
                }
            }
        })

        viewModel.otherAccount.observe(this, {
            viewModel.otherName.value = ""
        })
        viewModel.queryRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    viewModel.otherName.value = it.data?.real_name
                }
            }
        })
    }

    fun selectNum() {
        PagingFixedBathDialog(this@ZrDjActivity).apply {
            vm = viewModel
            ok = {
                viewModel.selectBath.value = it
                JyInputNumDialog(this@ZrDjActivity).apply {
                    ok = { num ->
                        viewModel.sNum.value = "${num}"
                        dismiss()
                    }
                }.show()
            }
        }.show()
    }

    override fun initViewModel(): ZrdjViewModel = createViewModel()

    fun lsjl() {
        startActivity(Intent(this, DjzrActivity::class.java))
    }
}